package org.smart4j.framework.util;

import com.sun.scenario.effect.Reflection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.spec.ECField;

/**
 * Created by Cai on 2017/7/27.
 */
public class ReflectionUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);

    /*create instance*/
    public static Object newInstance(Class<?> cls){
        Object instance;
        try {
            instance = cls.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("new instance failure", e);
            throw new RuntimeException(e);
        }
        return instance;
    }

    /*调用方法*/
    public static Object invokeMEthod(Object obj, Method method, Object...params){
        Object result;
        method.setAccessible(true);
        try {
            result = method.invoke(obj, params);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("invoke method failure", e);
            throw new RuntimeException(e);
        }
        return result;
    }

    /*设置成员变量的值*/
    public static void setFiled(Object obj, Field field, Object value){
        field.setAccessible(true);
        try {
            field.set(obj, value);
        } catch (IllegalAccessException e) {
            LOGGER.error("set field failure", e);
            throw new RuntimeException(e);
        }
    }

    /**/
}
